package com.ysh.smartplatform.security.service;



import com.ysh.smartplatform.mapper.UserMapper;
import com.ysh.smartplatform.pojo.BaseUser;
import com.ysh.smartplatform.pojo.SysUserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * Created by Jackiechan on 2022/9/15 14:28
 *
 * @author Jackiechan
 * @version 1.0
 * @since 1.0
 */
@Service
public class MyUserDetailService implements UserDetailsService {

    private UserMapper userMapper;

    @Autowired
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUserInfo user = userMapper.findUserByName(username);
        if (user == null) {
            //没有这个用户
            throw new UsernameNotFoundException("用户名或者密码错误");
        }
        ArrayList<GrantedAuthority> arrayList = new ArrayList<>();
        //参数1 当前的用户名,参数2 当前用户在数据库中的密码是什么,参数3,用户的权限,现在我们可能密码不对,所以没有必要立刻查询数据库
        return new BaseUser(username,user.getPassword(),arrayList, user.getuId());
    }
}
